package com.zhongxi.guoyuan.view.seller;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.libs.zxing.CaptureActivity;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardGoodInfoAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.LogisticsBaseResult;
import com.zhongxi.guoyuan.entity.seller.CoinCenterData;
import com.zhongxi.guoyuan.entity.seller.CoinCenterResult;
import com.zhongxi.guoyuan.entity.seller.EarningsInfo;
import com.zhongxi.guoyuan.entity.seller.ExpressCompany;
import com.zhongxi.guoyuan.entity.seller.OrderInfos;
import com.zhongxi.guoyuan.entity.seller.SilverPostOrderSearch;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.ImageUtils;
import com.zhongxi.guoyuan.utils.RegularTool;
import com.zhongxi.guoyuan.widget.view.MyScrollView;
import com.zhongxi.guoyuan.widget.xlistview.XListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SellerRewardCoinActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener, MyScrollView.OnScrollListener {

    private static final int SELLERPAYMONEY = 00;
    private static final int REQUESTVERIFY = 88;
    private static final int SELLERVERIFYCODE = 99;
    /**
     * scrollView 滑动时间
     */
    private static final long TIME = 200;
    private TextView tv_shopid;
    private TextView tvCoin;
    private TextView tvAllNum;
    private TextView tvCashNum;
    private TextView tvPostagePrice;

    private EditText etPhone;
    private ImageView tvQuery;
    private XListView lvList;
    private TextView tvAllOrderNum;
    private TextView tvPostNum;
    private TextView tvtimePrice;
    private String type;
    private TextView tvStore;
    private TextView tvStartTime;
    private TextView tvEndTime;

    private String startTime;
    private String endTime;
    private DatePickerDialog dialog;
    private RadioButton rb0, rb1, rb2, rb3, rb4;
    private List<OrderInfos> datas = new ArrayList<>();
    private List<JSONObject> orderCusObjectList = new ArrayList<>();
    private List<JSONObject> complaintsObjectList = new ArrayList<>();
    private List<JSONObject> mirrorInfoObjectList = new ArrayList<>();
    private List<JSONObject> userObjectList = new ArrayList<>();
    private RewardGoodInfoAdapter adapter;
    private String ordertype = "1";

    private boolean isLoadMore;
    private LinearLayout scrollBody;
    private LinearLayout linearBegin;
    private LinearLayout linearStop;
    private LinearLayout relaLayout;
    private boolean isMeasured;
    private int scrollHeight;

    private TextView et_hint_text;
    private TextView et_text_cancel;
    private TextView et_clear;

    private MyScrollView myScrollView;
    private boolean issearch = false;
    private Map<String, String> map = new HashMap<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            pos = msg.getData().getInt("pos");
            switch (msg.what) {
                case 1:
                    flag = msg.getData().getInt("flag", 0);
                    map.clear();
                    map.put("orderid", msg.getData().getString("orderid"));
                    map.put("shopid", msg.getData().getString("shopid"));
                    type = "prepareGoods";
                    getServer(Url.REWARD_PREPARE_GOODS, map, "", true, null, "post");
                    break;
                case 2:
                    String orderid1 = msg.getData().getString("orderid");
                    sendExpressage(orderid1);
                    break;
                case 3:
                    map.clear();
                    map.put("orderid", msg.getData().getString("orderid"));
                    type = "over";
                    getServer(Url.REWARD_OVER_PROPLEM, map, "", true, null, "post");
                    break;
            }
        }
    };
    private String expressNum;
    private int pos;
    private RatingBar rb;
    private String trim = "";
    private ImageView iv_verify;
    private RelativeLayout title;
    private int flag = 0;
    private AlertDialog alertDialog;

    private void sendExpressage(final String orderId) {
        try {
            final List<String> spList = new ArrayList<String>();
            final List<ExpressCompany> expressCompanies = new ArrayList<>();
            JSONObject express = new JSONObject(MyApplication.getInstance().getLogistics());
            Iterator<String> iterator = express.keys();
            while (iterator.hasNext()) {
                ExpressCompany expressCompany = new ExpressCompany();
                String key = iterator.next();
                String value = express.optString(key);
                expressCompany.setExpressNum(key);
                expressCompany.setExpressName(value);
                spList.add(value);
                expressCompanies.add(expressCompany);
            }
            AlertDialog.Builder adb = new AlertDialog.Builder(context);
            final AlertDialog dialog1 = adb.create();
            View view = View.inflate(context, R.layout.dialog_post_silver_send_good, null);
            ImageView ivCancel = (ImageView) view.findViewById(R.id.ivCancel);
            LoopView loopView = (LoopView) view.findViewById(R.id.loopView);
            loopView.setInitPosition(0);
            loopView.setNotLoop();
            loopView.setListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(int index) {
                    String s = spList.get(index);
                    for (ExpressCompany ec : expressCompanies) {
                        if (s.equals(ec.getExpressName())) {
                            expressNum = ec.getExpressNum();
                        }
                    }
                }
            });
            loopView.setItems(spList);
            final EditText et_expressNumber = (EditText) view.findViewById(R.id.et_expressNumber);
            TextView btSubmit = (TextView) view.findViewById(R.id.btSubmit);
            btSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String trim = et_expressNumber.getText().toString().trim();
                    if (TextUtils.isEmpty(expressNum)) {
                        showShortToast("快递公司不能为空");
                    } else if (TextUtils.isEmpty(trim)) {
                        showShortToast("快递单号不能为空");
                    } else {
                        dialog1.dismiss();
                        map.clear();
                        map.put("orderid", orderId);
                        map.put("shippercode", expressNum);
                        map.put("logisticscode", trim);
                        type = "sendExpress";
                        getServer(Url.REWARD_SEND_GOODS, map, "", true, null, "post");
                    }
                }
            });

            ivCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog1.dismiss();
                }
            });

            dialog1.setView(view);
            dialog1.show();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            myScrollView.fullScroll(MyScrollView.FOCUS_UP);
        }
    };

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_reward_coin);
        initTitle();
        tvTitle.setText("线上赚豆商家");
        iv_verify = (ImageView) findViewById(R.id.iv_verify);
        title = (RelativeLayout) findViewById(R.id.title);

        tvStore = (TextView) findViewById(R.id.tvStore);
        tv_shopid = (TextView) findViewById(R.id.tv_shopid);
        rb = (RatingBar) findViewById(R.id.rb);
        tvCoin = (TextView) findViewById(R.id.tvCoin);

        tvAllNum = (TextView) findViewById(R.id.tvAllNum);
        tvCashNum = (TextView) findViewById(R.id.tvCashNum);
        tvPostagePrice = (TextView) findViewById(R.id.tvPostagePrice);

        tvStartTime = (TextView) findViewById(R.id.tvStartTime);
        tvEndTime = (TextView) findViewById(R.id.tvEndTime);
        tvAllOrderNum = (TextView) findViewById(R.id.tvAllOrderNum);
        tvPostNum = (TextView) findViewById(R.id.tvPostNum);
        tvtimePrice = (TextView) findViewById(R.id.tvtimePrice);

        etPhone = (EditText) findViewById(R.id.etPhone);
        tvQuery = (ImageView) findViewById(R.id.tvQuery);

        et_hint_text = (TextView) findViewById(R.id.et_hint_text);
        et_text_cancel = (TextView) findViewById(R.id.et_text_cancel);
        et_clear = (TextView) findViewById(R.id.et_clear);

        myScrollView = (MyScrollView) findViewById(R.id.myScrollView);

        lvList = (XListView) findViewById(R.id.lvList);
        lvList.setPullRefreshEnable(false);
        lvList.setPullLoadEnable(true);


        rb0 = (RadioButton) findViewById(R.id.rb0);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);
        //实现上拉布局悬停
        initScrollPause();
    }

    private void initScrollPause() {
        //滑动置顶
        scrollBody = (LinearLayout) findViewById(R.id.scrollBody);
        linearBegin = (LinearLayout) findViewById(R.id.linearBegin);
        linearStop = (LinearLayout) findViewById(R.id.linearStop);
        relaLayout = (LinearLayout) findViewById(R.id.relaLayout);
        ViewTreeObserver observer = relaLayout.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (!isMeasured) {
                    scrollHeight = relaLayout.getMeasuredHeight();
                    isMeasured = true;
                }
                return true;
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new RewardGoodInfoAdapter(context, datas, handler);
        lvList.setAdapter(adapter);
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(now);
        tvStartTime.setText(dateNowStr);
        tvEndTime.setText(dateNowStr);
        startTime = endTime = dateNowStr;
        getMallInfo();
    }

    /**
     * 控制台信息
     */
    private void getMallInfo() {
        type = "info";
        getServer(Url.REWARD_INFO, null, "", true, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        iv_verify.setOnClickListener(this);
        myScrollView.setOnScrollListener(this);
        ivBack.setOnClickListener(this);
        tvStartTime.setOnClickListener(this);
        tvEndTime.setOnClickListener(this);
        tvQuery.setOnClickListener(this);
        tvCoin.setOnClickListener(this);

        rb0.setOnClickListener(this);
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
        rb4.setOnClickListener(this);


        etPhone.setOnClickListener(this);
        et_clear.setOnClickListener(this);
        et_text_cancel.setOnClickListener(this);

        lvList.setOnItemClickListener(this);
        lvList.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() { //下拉刷新
                isLoadMore = false;
                pageNum = 1;
                getSilverDetail();
            }

            @Override
            public void onLoadMore() {   //加载更多
                isLoadMore = true;
                pageNum++;
                getSilverDetail();
            }
        });

        etPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                if (b) {
                    et_text_cancel.setVisibility(View.VISIBLE);
                    et_text_cancel.setText("取消");
                    et_clear.setVisibility(View.GONE);
                    et_hint_text.setVisibility(View.GONE);
                }
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 0) {
                    et_clear.setVisibility(View.VISIBLE);
                    et_text_cancel.setText("搜索");
                } else {
                    et_clear.setVisibility(View.GONE);
                    et_text_cancel.setText("取消");
                }
                et_hint_text.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.iv_verify:
                showVerifyPopupwindow();
                break;
            case R.id.tvStartTime:
                showDateDialog(0);
                break;
            case R.id.tvEndTime:
                showDateDialog(1);
                break;
            case R.id.tvQuery:
                pageNum = 1;
                trim = etPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(trim)) {
                    if (!RegularTool.isPhoneNumberValid(trim)) {
                        showShortToast("请输入正确的手机号");
                        break;
                    }
                } else {
                    etPhone.setText("");
                    et_hint_text.setVisibility(View.VISIBLE);
                    et_clear.setVisibility(View.GONE);
                    et_text_cancel.setVisibility(View.GONE);
                }
                getSilverDetail();
                //隐藏软键盘
                issearch = true;
                CommonTool.hideSoft(etPhone, this);
                break;
            case R.id.et_clear:

                etPhone.setText("");
                et_hint_text.setVisibility(View.VISIBLE);
                et_text_cancel.setText("取消");
                et_text_cancel.setVisibility(View.VISIBLE);
                CommonTool.hideSoft(etPhone, context);
                break;
            case R.id.et_text_cancel:
                pageNum = 1;
                trim = etPhone.getText().toString().trim();

                if (!TextUtils.isEmpty(trim)) {
                    if (!RegularTool.isPhoneNumberValid(trim)) {
                        showShortToast("请输入正确的手机号");
                        break;
                    }
                } else {
                    etPhone.setText("");
                    et_hint_text.setVisibility(View.VISIBLE);
                    et_clear.setVisibility(View.GONE);
                    et_text_cancel.setVisibility(View.GONE);
                }
                getSilverDetail();

                issearch = true;
                CommonTool.hideSoft(etPhone, this);
                break;
            case R.id.etPhone:
                et_text_cancel.setVisibility(View.VISIBLE);
                break;
            case R.id.rb0:
                if (scrollBody.getParent() != linearBegin) {
                    linearStop.removeView(scrollBody);
                    linearBegin.addView(scrollBody);
//                    myScrollView.fullScroll(MyScrollView.FOCUS_UP);
                    handler.postDelayed(runnable, TIME);
                }
                ordertype = "1";
                pageNum = 1;
                rb0.setChecked(true);
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                getSilverDetail();
                break;
            case R.id.rb1:   //邮寄订单
                if (scrollBody.getParent() != linearBegin) {
                    linearStop.removeView(scrollBody);
                    linearBegin.addView(scrollBody);
//                    myScrollView.fullScroll(MyScrollView.FOCUS_UP);
                    handler.postDelayed(runnable, TIME);
                }
                ordertype = "2";
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(true);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                getSilverDetail();
                break;
            case R.id.rb2:    //邮寄订单(问题件)
                if (scrollBody.getParent() != linearBegin) {
                    linearStop.removeView(scrollBody);
                    linearBegin.addView(scrollBody);
//                    myScrollView.fullScroll(MyScrollView.FOCUS_UP);
                    handler.postDelayed(runnable, TIME);
                }
                ordertype = "3";
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(false);
                rb2.setChecked(true);
                rb3.setChecked(false);
                rb4.setChecked(false);
                getSilverDetail();
                break;
            case R.id.rb3:      //邮寄订单
                if (scrollBody.getParent() != linearBegin) {
                    linearStop.removeView(scrollBody);
                    linearBegin.addView(scrollBody);
                    handler.postDelayed(runnable, TIME);
                }
                ordertype = "4";
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(true);
                rb4.setChecked(false);
                getSilverDetail();
                break;
            case R.id.rb4:     //邮寄+自提
                if (scrollBody.getParent() != linearBegin) {
                    linearStop.removeView(scrollBody);
                    linearBegin.addView(scrollBody);
                    handler.postDelayed(runnable, TIME);
                }
                ordertype = "5";
                pageNum = 1;
                rb0.setChecked(false);
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(true);
                getSilverDetail();
                break;
            case R.id.tvCoin:
                Intent intent = new Intent(context, SellerCoinExchangeActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void showVerifyPopupwindow() {
        View view = View.inflate(context, R.layout.pop_verify, null);
        final PopupWindow pop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        LinearLayout rl_message = (LinearLayout) view.findViewById(R.id.rl_message);
        LinearLayout rl_dialogue = (LinearLayout) view.findViewById(R.id.rl_dialogue);
        rl_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
                showVerifyDialog();
            }
        });
        rl_dialogue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
//                startAct(PayMoneyActivity.class);
                //跳到扫描二维码页面
                Intent payIntent = new Intent(context, CaptureActivity.class);
                //startActivity(intent);
                //跳液面需要返回一个结果
                startActivityForResult(payIntent, SELLERPAYMONEY);
            }
        });
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        int screenWidth = ImageUtils.getScreenWidth(context);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int viewMeasuredWidth = view.getMeasuredWidth();
        title.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int titleMeasuredHeight = title.getMeasuredHeight();
        iv_verify.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int iv_verifyMeasuredHeight = iv_verify.getMeasuredHeight();
        pop.showAtLocation(iv_verify, Gravity.TOP | Gravity.START, screenWidth - viewMeasuredWidth, titleMeasuredHeight + iv_verifyMeasuredHeight);
    }

    private void showVerifyDialog() {
        AlertDialog.Builder adb = new AlertDialog.Builder(context);
        alertDialog = adb.create();
        View dialogView = View.inflate(context, R.layout.dialog_verify, null);
        ImageView iv_dimess = (ImageView) dialogView.findViewById(R.id.iv_dimess);
        final EditText et_verifyId = (EditText) dialogView.findViewById(R.id.et_verifyId);
        TextView btSubmit = (TextView) dialogView.findViewById(R.id.btSubmit);
        iv_dimess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = et_verifyId.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    showShortToast("请输入您正确的提货验证码");
                } else {
                    Intent intent = new Intent(context, SellerVerifyActivity.class);
                    intent.putExtra("trim", trim);
                    startActivityForResult(intent, REQUESTVERIFY);
                }
            }
        });
        alertDialog.setView(dialogView);
        alertDialog.show();
    }

    private void showDateDialog(final int index) {
        Calendar c = Calendar.getInstance();
        dialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String mon = month + 1 + "";
                String text = "";
                if (month + 1 < 10 && dayOfMonth < 10) {
                    text = year + "-0" + mon + "-0" + dayOfMonth;
                } else if (month + 1 < 10 && dayOfMonth >= 10) {
                    text = year + "-0" + mon + "-" + dayOfMonth;
                } else if (month + 1 >= 10 && dayOfMonth < 10) {
                    text = year + "-" + mon + "-0" + dayOfMonth;
                } else {
                    text = year + "-" + mon + "-" + dayOfMonth;
                }
                if (index == 0) {
                    tvStartTime.setText(text);
                    startTime = text;
                } else if (index == 1) {
                    tvEndTime.setText(text);
                    endTime = text;
                }
                getTimeInfo();
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.setTitle("请选择时间：");
        dialog.getDatePicker().setCalendarViewShown(false);
        dialog.show();
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("info".equals(type)) {
                CoinCenterResult result = gson.fromJson(response, CoinCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (null != result.getEarningsInfo() && !"".equals(result.getEarningsInfo())) {
                    getAllCount(result.getEarningsInfo());
                }
                if (null != result.getData() && !"".equals(result.getData())) {
                    initInfo(result.getData());
                }
                getTimeInfo();
            } else if ("timeInfo".equals(type)) {
                CoinCenterResult result = gson.fromJson(response, CoinCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (null != result.getEarningsInfo() && !"".equals(result.getEarningsInfo())) {
                    EarningsInfo earningsInfo = result.getEarningsInfo();
                    if (null != earningsInfo.getOrdernum() && !"".equals(earningsInfo.getOrdernum())) {
                        tvAllOrderNum.setText(earningsInfo.getOrdernum());
                    } else {
                        tvAllOrderNum.setText("");
                    }
                    if (null != earningsInfo.getShouldprice() && !"".equals(earningsInfo.getShouldprice())) {
                        tvPostNum.setText(earningsInfo.getShouldprice());
                    } else {
                        tvPostNum.setText("");
                    }
                    if (null != earningsInfo.getPostage() && !"".equals(earningsInfo.getPostage())) {
                        tvtimePrice.setText(earningsInfo.getPostage());
                    } else {
                        tvtimePrice.setText("");
                    }
                }
                getSilverDetail();
            } else if ("silverList".equals(type)) {
                SilverPostOrderSearch postOrderSearch = gson.fromJson(response, SilverPostOrderSearch.class);
                MyApplication.getInstance().setSpValue(postOrderSearch.getSafeobj());
                if (isLoadMore) {
                    isLoadMore = false;
                    if (postOrderSearch.getOrderInfo() == null || postOrderSearch.getOrderInfo().size() == 0) {
                        showShortToast("没有加载更多了");
                        isFinish();
                        pageNum--;
                        return;
                    }
                }
                if (pageNum == 1) {
                    datas.clear();
                    orderCusObjectList.clear();
                    complaintsObjectList.clear();
                    mirrorInfoObjectList.clear();
                    userObjectList.clear();
                }
                if (postOrderSearch.getOrderInfo() != null && postOrderSearch.getOrderInfo().size() > 0) {
                    List<OrderInfos> orderInfoses = postOrderSearch.getOrderInfo();
                    if (orderInfoses.size() > 0) {
                        datas.addAll(orderInfoses);
                    }
                    JSONObject responseObject = new JSONObject(response);
                    JSONObject orderCusObject = responseObject.optJSONObject("orderCus");//邮寄
                    if (null != orderCusObject && orderCusObject.length() > 0) {
                        orderCusObjectList.add(orderCusObject);
                        adapter.setOrderCus(orderCusObjectList);
                    }
                    JSONObject complaintsObject = responseObject.optJSONObject("complaints");
                    if (null != complaintsObject && complaintsObject.length() > 0) {
                        complaintsObjectList.add(complaintsObject);
                        adapter.setComplaint(complaintsObjectList);
                    }
                    JSONObject mirrorInfoObject = responseObject.optJSONObject("mirrorInfo");
                    if (null != mirrorInfoObject && mirrorInfoObject.length() > 0) {
                        mirrorInfoObjectList.add(mirrorInfoObject);
                        adapter.setMirrorInfo(mirrorInfoObjectList);
                    }
                    JSONObject userObject = responseObject.optJSONObject("user");
                    if (null != userObject && userObject.length() > 0) {
                        userObjectList.add(userObject);
                        adapter.setUserInfo(userObjectList);
                    }
                    adapter.setOrderType(ordertype);
                } else {
                    if (pageNum > 1) {
                        pageNum--;
                    }
                    if (issearch && trim.length() > 0) {
                        showShortToast("抱歉，搜索不到与该号码相关的订单");
                    }
                }
                issearch = false;
                adapter.notifyDataSetChanged();
                isFinish();
            } else if ("prepareGoods".equals(type)) {
                BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
                MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
                if (1 == flag) {
                    datas.get(pos).setOrderstatus((short) 1);
                } else {
                    datas.get(pos).setPoststatus("1");
                }
                adapter.notifyDataSetChanged();
            } else if ("sendExpress".equals(type)) {
                BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
                MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
                showShortToast("快递发货成功");
                datas.remove(pos);
                adapter.notifyDataSetChanged();
            } else if ("over".equals(type)) {
                BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
                MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
                showShortToast("投诉订单已解决完成");
                datas.remove(pos);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getTimeInfo() {
        if (CommonTool.TimeCompare(startTime, endTime)) {
            type = "timeInfo";
            map.clear();
            map.put("stime", startTime);
            map.put("etime", endTime);
            getServer(Url.REWARD_DATA, map, "", true, null, "post");
        } else {
            showShortToast("结束时间不能小于开始时间");
        }

    }

    private void getAllCount(EarningsInfo earningsInfo) {
        if (null != earningsInfo.getOrdernum() && !"".equals(earningsInfo.getOrdernum())) {
            tvAllNum.setText(earningsInfo.getOrdernum());
        } else {
            tvAllNum.setText("");
        }
        if (null != earningsInfo.getShouldprice() && !"".equals(earningsInfo.getShouldprice())) {
            tvCashNum.setText(earningsInfo.getShouldprice());
        } else {
            tvCashNum.setText("");
        }
        if (null != earningsInfo.getPostage() && !"".equals(earningsInfo.getPostage())) {
            tvPostagePrice.setText(earningsInfo.getPostage());
        } else {
            tvPostagePrice.setText("");
        }
    }

    /**
     * 初始化控制台数据
     *
     * @param data
     */
    private void initInfo(CoinCenterData data) {
        try {
            if (null != data.getName() && !"".equals(data.getName())) {
                tvStore.setText(data.getName());
            } else {
                tvStore.setText("");
            }
            if (null != data.getId() && !"".equals(data.getId())) {
                tv_shopid.setText(data.getId());
            } else {
                tv_shopid.setText("");
            }
            if (null != data.getStarlevel() && !"".equals(data.getStarlevel())) {
                rb.setRating(Float.parseFloat(data.getStarlevel()));
            } else {
                rb.setRating(1.5f);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getSilverDetail() {
        type = "silverList";
        String str = "&page=" + pageNum + "&ordertype=" + ordertype + "&userphone=" + trim;
        getServer(Url.REWARD_BUSINESSLIST, null, str, true, null, "get");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (9002 == result.getCode()) {
                showShortToast("未登录");
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardcoincredentials", "").commit();
                finish();
            } else if (9003 == result.getCode()) {
                showShortToast("店铺已关闭");
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardcoincredentials", "").commit();
                finish();
            } else if (9004 == result.getCode()) {
                showShortToast("超管不能使用该接口");
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardcoincredentials", "").commit();
                finish();
            } else if (5014 == result.getCode()) {
                showShortToast("订单信息缺少 镜像信息，无效订单");
            } else if (9102 == result.getCode()) {
                LogisticsBaseResult logistics = gson.fromJson(response, LogisticsBaseResult.class);
                showShortToast(logistics.getData().getErrorMsg());
            } else if (2015 == result.getCode()) {
                showShortToast("订单镜像丢失");
            } else if (2016 == result.getCode()) {
                showShortToast("订单定制信息已丢失");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (scrollBody.getParent() != linearBegin) {
            linearStop.removeView(scrollBody);
            linearBegin.addView(scrollBody);
        }
        if (position - 1 == adapter.getNotification()) {
            adapter.setNotification(-1);
        } else {
            adapter.setNotification(position - 1);
            myScrollView.scrollTo(0, view.getHeight() * position);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onScroll(int scrollY) {
        if (scrollY >= scrollHeight) {
            if (scrollBody.getParent() != linearStop) {
                linearBegin.removeView(scrollBody);
                linearBegin.setLayoutParams(new LinearLayout.LayoutParams(scrollBody.getMeasuredWidth(), scrollBody.getMeasuredHeight()));
                linearStop.addView(scrollBody);
            }
        } else {
            if (scrollBody.getParent() != linearBegin) {
                linearStop.removeView(scrollBody);
                linearBegin.addView(scrollBody);
            }
        }
    }

    @Override
    public void scrollBottom() {
    }

    private void isFinish() {
        lvList.stopRefresh();
        lvList.stopLoadMore();
        lvList.setRefreshTime("刚刚");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == SELLERPAYMONEY) {
            //获取解析的数据
            String text = data.getStringExtra("text");
            Intent intent = new Intent(context, SellerVerifyActivity.class);
            intent.putExtra("text", text);
            startActivityForResult(intent, SELLERVERIFYCODE);
//            handleResult(text);
        } else if (requestCode == REQUESTVERIFY) {
            alertDialog.dismiss();
            getSilverDetail();
        } else if (requestCode == SELLERVERIFYCODE) {
            getSilverDetail();
        }
    }
}
